﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace T01WPF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {//设置fataGrid空间的数据源
            GameUserDataGrid.ItemsSource = UserDao.LoadData();

        }

        private void UserEvent_Click(object sender,RoutedEventArgs e)
        {
            switch ((sender as Control).Name)
            {
                case "About":
                    Debug.WriteLine("About");
                    MessageBox.Show("这是MYSQL数据访问测试程序", "关于");
                    break;
                case "Exit":
                    Debug.WriteLine("Exit");
                    this.Close();
                    Application.Current.Shutdown(0);
                    break;
                case "UserInsert":
                    Debug.WriteLine("UserInsert");
                    var insertDialog = new SaveUserDialog();
                    insertDialog.ActinType = "insert";
                    insertDialog.SaveaUser.Content = "新增内容";
                    if (insertDialog.ShowDialog() == true)
                    {
                        GameUserDataGrid.ItemsSource = UserDao.LoadData();
                    }
                    break;
                case "UserFind":
                    Debug.WriteLine("UserFind");
                    var findDialog = new FindUserDialog();
                    if (findDialog.ShowDialog() == true)
                    {
                        GameUserDataGrid.ItemsSource = findDialog.users;
                    }
                    break;
                case "UserUpdate":
                    Debug.WriteLine("UserUpdate");
                    if (GameUserDataGrid.SelectedItem is User)
                    {
                        var user = GameUserDataGrid.SelectedItem as User;
                        var updateDialog = new SaveUserDialog(user);
                        updateDialog.ActinType = "update";
                        updateDialog.SaveaUser.Content = "修改用户";
                        if (updateDialog.ShowDialog() == true)
                        {
                            GameUserDataGrid.ItemsSource = UserDao.LoadData();
                        }

                    }
                    break;
                case "UserDelete":
                    Debug.WriteLine("UserDelete");
                    MessageBoxResult confirmDelete = MessageBox.Show("确认要删除吗？", "提示",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question);
                    if (confirmDelete == MessageBoxResult.Yes)
                    {
                        if (GameUserDataGrid.SelectedItem is User)
                        {
                            var user = GameUserDataGrid.SelectedItem as User;
                            UserDao.Delete(user.Id);
                            GameUserDataGrid.ItemsSource = UserDao.LoadData();
                        }
                    }
                    break;
            }

        }

    }
}
